package com.bigShen.spring.mybatis;

import com.bigShen.spring.mybatis.config.DataConfig;
import com.bigShen.spring.mybatis.dao.UserInfoMapper;
import com.bigShen.spring.mybatis.entity.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

/**
 * @Author eddy.shen
 * @Date 2018/12/16 20:49
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DataConfig.class)
public class SpringMyBatisJavaConfigHelloWorld {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Test
    public void testSelectUnTransaction() {

       testSelect("No Transaction");

    }


    @Test
    @Transactional
    public void testSelectTransaction() {

        testSelect("Transaction");

    }


    /**
     * 自定义事物不起作用???
     * @throws Exception
     */
    @Test
    @Transactional
    public void testDefineTransaction() throws Exception {
        String transactionManagerName = "txManager";
        //DataSourceTransactionManager txManager = (DataSourceTransactionManager) WebContextUtil.getBean(transactionManagerName);
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DataConfig.class);
        DataSourceTransactionManager txManager = (DataSourceTransactionManager) applicationContext.getBean(transactionManagerName);

        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = txManager.getTransaction(def);

        try {

            UserInfo userInfo = new UserInfo();
            userInfo.setUser_id(666L);
            userInfo.setUser_name("bigShen");
            userInfo.setPassword("pwd");
            userInfo.setAccount("BS");
            userInfo.setFlag(Boolean.FALSE);
            userInfo.setStatus(Boolean.FALSE);
            userInfoMapper.insertUser(userInfo);
            userInfoMapper.insertUser(userInfo);
            txManager.commit(status);
        } catch (Exception ex) {
            txManager.rollback(status);
            throw ex;
        }

    }

    @Test
    @Transactional
    public void testSpringTransaction() throws Exception {
        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setUser_id(333L);
            userInfo.setUser_name("bigShen");
            userInfo.setPassword("pwd");
            userInfo.setAccount("BS");
            userInfo.setFlag(Boolean.FALSE);
            userInfo.setStatus(Boolean.FALSE);
            userInfoMapper.insertUser(userInfo);

            userInfoMapper.insertUser(userInfo);
        }
        catch (Exception ex) {
            throw ex;
        }

    }

    private void testSelect(String msg) {

        Long start = System.currentTimeMillis();

        String account = "004255";
        List<UserInfo> userInfos = userInfoMapper.selectUserByAccount(account);
        System.out.println(userInfos);
        Long end1 = System.currentTimeMillis();
        System.out.println(String.format("%s first cost : %s", msg, (end1 - start)));

        userInfos = userInfoMapper.selectUserByAccount(account);
        System.out.println(userInfos);
        Long end2 = System.currentTimeMillis();
        System.out.println(String.format("%s first cost : %s", msg, (end2 - end1)));

        userInfos = userInfoMapper.selectUserByAccount(account);
        System.out.println(userInfos);
        Long end3 = System.currentTimeMillis();
        System.out.println(String.format("%s first cost : %s", msg, (end3 - end2)));

    }


}
